import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex);

/* 存储库 */
export default new Vuex.Store({
    state: {
        /* 认证token */
        token:"",
        /* 用户信息 */
        user:{}
    },
    getters: {
        token(state){
            if(state.token){
                return state.token;
            }
            if(localStorage.token){
                state.token = localStorage.token;
                return localStorage.token;
            }
            return null;
        },
        user(state){
            return state.user;
        }
    },
    mutations: {
        /* 设置token */
        setToken(state,token){
            console.log("设置token："+token);
            state.token = token;
            localStorage.token = token
        },
        /* 设置用户信息 */
        setUser(state,user){
            state.user = user;
        }
    },
    actions: {
        login(context){

        },
        /* 加载用户信息 */
        loadUser(context){
            console.log("加载用户信息");
            axios.get("/indentity/user/info")
                .then(({data})=>{
                    console.log(data);
                    /* 存储用户信息 */
                    context.commit("setUser",data);
                })
        }
    },
    modules: {
        //
    }
})
